package thomasweb.spring.data.config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.flywaydb.core.Flyway;
import org.h2.server.web.WebServlet;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import thomasweb.spring.data.entity.statistic.entity.LoginRecords;
import thomasweb.spring.data.entity.statistic.repo.LoginRecordsRepository;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
		entityManagerFactoryRef="statisticEntityManagerFactory",
		transactionManagerRef="statisticTransactionManager",
		basePackageClasses={LoginRecordsRepository.class})
@EntityScan(basePackageClasses={LoginRecords.class})
public class StatisticDataSourceConfig {
	
	private final Log LOG = LogFactory.getLog(StatisticDataSourceConfig.class);
	/**
	 * H2 Data Source
	 * @return
	 */
	@Bean(name="statisticDataSource")
	@ConfigurationProperties(prefix="statistic.datasource.log")
	public DataSource getStatisticDS(){
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name="statisticEntityManagerFactory")
	public LocalContainerEntityManagerFactoryBean statisticEntityManagerFactory(
			EntityManagerFactoryBuilder builder,
			@Qualifier("statisticDataSource") DataSource dataSource){
		 return builder.dataSource(dataSource).packages(LoginRecords.class).persistenceUnit("statistic").build();
	}
	
	@Bean(name="statisticTransactionManager")
	public PlatformTransactionManager statisticTransactionManager(
			@Qualifier("statisticEntityManagerFactory") EntityManagerFactory entityManagerFactory){
		return  new JpaTransactionManager(entityManagerFactory);
	}
	
	@Bean
	@Qualifier("statisticFlyway")
	public Flyway flywayForStatisticDB(){
		Flyway flyway = new Flyway();
		flyway.setDataSource(getStatisticDS());
		flyway.setBaselineOnMigrate(true);
		flyway.setLocations("classpath:db/migration/statistic/");
		flyway.setValidateOnMigrate(false);
		flyway.migrate();
		LOG.info("statisticFlyway intiated");
		return flyway;
	} 
	
	@Bean
	public ServletRegistrationBean h2servletRegistration() {
	    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
	    registration.addUrlMappings("/h2-console/*");
	    return registration;
	}
}
